package edu.washington.math.sage;

import java.awt.Color;
import java.awt.Container;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JFrame;

@SuppressWarnings("serial")
public class PyPhysSimGui extends JFrame implements WindowListener{
	PrintStream myPrintStream;
	Map<String,GeneralPath> myPaths;
	Map<String,Color> myColors;
    public PyPhysSimGui(String arg) {
	   super(arg);
	   myPrintStream=System.out;
	   myPaths = new HashMap<String,GeneralPath>();
	   myColors = new HashMap<String,Color>();
	   this.addWindowListener(this);
    }
    public static void main(String[] args) {
       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	   PyPhysSimGui guiFrame = new PyPhysSimGui("PyPhysSim");
	   guiFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	   PyPhysSimGUIPanel ppspanel=new PyPhysSimGUIPanel(guiFrame);
	   Container contentPane=guiFrame.getContentPane();
	   contentPane.add(ppspanel);
	   String message="test";
   	   guiFrame.setVisible(true);
   	   //guiFrame.setSize(500, 500);
   	   guiFrame.pack();
       while (true){
    	   try {
  	         message = br.readLine();
  	         //I used http://forums.sun.com/thread.jspa?threadID=544642 as a resource on String splitting
  	         String[] tokens = message.split(" ");
  	         if (tokens[0].equals("CREATE_SHAPE")){
  	  	         String name=tokens[1];
  	  	         GeneralPath newPath = new GeneralPath();
  	  	         Color color = new Color(new Integer(tokens[2]),new Integer(tokens[3]),new Integer(tokens[4]));
  	  	         int index=5;
  	  	         while (index<tokens.length){
  	  	        	 if (tokens[index].equals("C")){//Cubic curve to
  	  	        		 newPath.curveTo(new Double(tokens[index+1]), new Double(tokens[index+2]),
  	  	        				 new Double(tokens[index+3]), new Double(tokens[index+4]),
  	  	        				 new Double(tokens[index+5]), new Double(tokens[index+6]));
  	  	        		 index+= 6+1;
  	  	        	 } else if (tokens[index].equals("L")){//Line to
  	  	        		newPath.lineTo(new Double(tokens[index+1]), new Double(tokens[index+2]));
  	  	        		 index+=2+1;
  	  	        	 } else if (tokens[index].equals("M")){//Move to
   	  	        		newPath.moveTo(new Double(tokens[index+1]), new Double(tokens[index+2]));
  	  	        		 index+=2+1;
  	  	        	 } else if (tokens[index].equals("Q")){//Quad to
  	  	        	 newPath.quadTo(new Double(tokens[index+1]), new Double(tokens[index+2]),
	  	        				 new Double(tokens[index+3]), new Double(tokens[index+4]));
  	  	        		 index+=4+1;
  	  	        	 } else if (tokens[index].equals("Z")){//Close path
  	  	        		 newPath.closePath();
  		  	        	guiFrame.myPaths.put(name, newPath);
  		  	        	guiFrame.myColors.put(name,color);
  		  	        	index+=1;
  	  	        	 }
  	  	         }
  	         } else if (tokens[0].equals("DELETE_SHAPE")){
  	        	String name=tokens[1];
  	        	guiFrame.myPaths.remove(name);
  	         }else if (tokens[0].equals("TRANSLATE_SHAPE")){
  	        	String name=tokens[1];
  	        	float tx = new Float(tokens[2]);//horizontal translation
  	        	float ty = new Float(tokens[3]);//vertical translation
  	        	float theta = new Float(tokens[4]);//rotation angle
  	        	//anchorx,archory, are the coords of the anchor of rotation
  	        	float anchorx = new Float(tokens[5]);
  	        	float anchory = new Float(tokens[6]);
  	        	guiFrame.myPaths.get(name).transform(AffineTransform.getRotateInstance(theta, anchorx, anchory));
  	        	guiFrame.myPaths.get(name).transform(AffineTransform.getTranslateInstance(tx, ty));
  	         }else if (tokens[0].equals("REPAINT")){
  	        	ppspanel.myDP.repaint();
  	         }
  	      } catch (IOException ioe) {
  	         //System.out.println("IO error trying to message from Python!");
  	    	guiFrame.myPrintStream.println("WINDOW_CLOSED");
  	         System.exit(1);
  	      }
       }
	   
	      //contentPane.setLayout(new BorderLayout());
	      //contentPane.add(new JLabel(userName),BorderLayout.CENTER);
          //guiFrame.pack();
   }
	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	//resource: http://forums.sun.com/thread.jspa?threadID=722996
	@Override
	public void windowClosed(WindowEvent arg0) {
		myPrintStream.println("WINDOW_CLOSED");
		System.exit(0);	
	}
	@Override
	public void windowClosing(WindowEvent arg0) {
		// TODO Auto-generated method stub
	}
	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
